//
//  LXLoanPlanTableViewCell.m
//  ZuLinProject
//
//  Created by bancheng on 2022/9/14.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXLoanPlanTableViewCell.h"
#import "LXLoanModel.h"
@interface LXLoanPlanTableViewCell ()
@property (nonatomic)NSMutableArray *data;

@end


@implementation LXLoanPlanTableViewCell


int loanHeight = 40;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier WithData:(nonnull NSMutableArray *)data IsShow:(BOOL)isShow{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.data = data;

        
        
        [self.contentView addSubview:self.titleLab];
        
        
        [self.contentView addSubview:self.contentLab];
        
        NSArray *ary = @[@"期数",@"应付日期",@"金额",@"备注",@"操作"];
        
        CGFloat topX = 0;
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(15, self.titleLab.Y_height + 10, ScreenWidth - 30 , loanHeight)];
        topView.backgroundColor = ColorWithF5F5F5;
        [self.contentView addSubview:topView];
        for (int i = 0; i < ary.count; i++) {
    
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(topX, 0, topView.width/ary.count, topView.height)];
            label.textColor = ColorWith333333;
            label.font = [UIFont systemFontOfSize:15];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = ary[i];
            [topView addSubview:label];
            
            topX = label.X_width;
        }
        
        
        CGFloat X = 15;
        CGFloat Y = topView.Y_height;
        CGFloat W = topView.width/ary.count;
        
        NSString *totalRent = @"0.0";
        
        BOOL isNull = NO;
        
        if (data.count == 0) {
            isNull = YES;
            NSArray *ary = @[
                
                @{
                    @"fundType":@"--",
                    @"exceptPaidDate":@"--",
                    @"amount":@"--",
                    @"remark":@"--",
                    @"period":@"",
                },
                
            ];
            data = [LXLoanModel mj_objectArrayWithKeyValuesArray:ary];
        }
        
        
        for (int i = 0; i < data.count; i++) {
            
            LXLoanModel *model = data[i];
            
            totalRent = [NSString calculateByadding:model.amount secondNumber:totalRent];

            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(X, Y, topView.width , loanHeight)];
            [self.contentView addSubview:view];
            [view addLeftBorderWithColor:[UIColor colorWithHexString:@"#F2F2F2"] andWidth:1];
            [view addRightBorderWithColor:[UIColor colorWithHexString:@"#F2F2F2"] andWidth:1];
            [view addBottomBorderWithColor:[UIColor colorWithHexString:@"#F2F2F2"] andWidth:1];

            
    
            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, W, view.height)];
            label1.textColor = ColorWith666666;
            label1.textAlignment = NSTextAlignmentCenter;
            label1.font = Fond13;
            [view addSubview:label1];
            if (isNull) {
                label1.text = @"--";
            }
            else {
                label1.text = [NSString stringWithFormat:@"%@/%@",@(i+1),@(data.count)];
            }
            
            
            UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(label1.X_width, 0, W, view.height)];
            label2.textColor = ColorWith666666;
            label2.font = Fond12;
            label2.adjustsFontSizeToFitWidth = YES;
            label2.textAlignment = NSTextAlignmentCenter;
            label2.text = model.exceptPaidDate;
            [view addSubview:label2];
            
            UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(label2.X_width, 0, W, view.height)];
            label3.textColor = ColorWith666666;
            label3.font = Fond13;
            label3.adjustsFontSizeToFitWidth = YES;
            label3.textAlignment = NSTextAlignmentCenter;
            label3.text = model.amount;
            [view addSubview:label3];
            
            UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(label3.X_width, 0, W, view.height)];
            label4.textColor = ColorWith666666;
            label4.adjustsFontSizeToFitWidth = YES;
            label4.font = Fond13;
            label4.textAlignment = NSTextAlignmentCenter;
            label4.text = model.remark;
            [view addSubview:label4];
            
            UIButton *label5 = [[UIButton alloc] initWithFrame:CGRectMake(label4.X_width, 0, W, view.height)];
            label5.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            if (isNull || isShow) {
                [label5 setImage:[UIImage imageNamed:@"car_information_edit_ash"] forState:UIControlStateNormal];
            }
            else {
                [label5 setImage:[UIImage imageNamed:@"carDetail_修改信息"] forState:UIControlStateNormal];
                [label5 whenTapped:^{
                                    
                    if (self.clickBtnAction) {
                        self.clickBtnAction(model);
                    }
                }];
            }
            
            [view addSubview:label5];

            Y = view.Y_height;
        }
        
        if (isNull) {
            self.contentLab.text = @"共计应还0期；总计0元";
        }
        else {
            self.contentLab.text = [NSString stringWithFormat:@"共计算应还%@期；总计%@元",@(data.count),totalRent];
        }

       
        self.contentLab.Y = Y + 10;

    }
    return self;
    
    
    
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 18, 200, 18)];
        _titleLab.textColor = ColorWith333333;
        _titleLab.font = Fond16;
    }
    return _titleLab;
}
- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] initWithFrame:CGRectMake(15, self.height - 28, ScreenWidth - 30, 18)];
        _contentLab.textColor = ColorWith333333;
        _contentLab.font = Fond14;
    }
    return _contentLab;
}
+ (CGFloat)getHeight:(NSArray *)ary{
    if (ary.count == 0) {
        return 170;
    }
    else {
        return ary.count * loanHeight  + 43 + 42 + loanHeight;
    }
}
@end
